package com.heima.admin.services.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.services.AdUserService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vos.AdUserVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto) {

        if(dto == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码不能为空");
        }
        AdUser user = getOne(Wrappers.<AdUser>lambdaQuery()
                .eq(AdUser::getName, dto.getName()));
        if(user == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户名不存在");
        }
       String inputPwd  = DigestUtils.md5DigestAsHex((dto.getPassword() + user.getSalt()).getBytes());
        if(inputPwd.equals(user.getPassword())){
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        if (user.getStatus().intValue()!=9) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"用户状态异常");
        }
        user.setCreatedTime(new Date());
        updateById(user);

        String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
        AdUserVO adUserVO = new AdUserVO();
        BeanUtils.copyProperties(user, adUserVO);
        Map result = new HashMap();
        result.put("user",user);
        result.put("token",token);

        return ResponseResult.okResult(result);
    }

    @Override
    public ResponseResult register(AdUser user) {
        if(StringUtils.isEmpty(user.getName())||StringUtils.isEmpty(user.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名密码不能为空");
        }
        String salt = RandomStringUtils.randomAlphanumeric(8);
        String pwd=user.getPassword();
        user.setPassword(DigestUtils.md5DigestAsHex((pwd+salt).getBytes()));
        user.setSalt(salt);
        user.setCreatedTime(new Date());
        user.setStatus(9);
        save(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
